package com.qf.ability.message.handler;

import com.alibaba.fastjson.JSON;
import com.qf.ability.message.entity.Message;
import io.netty.channel.ChannelHandler;
import io.netty.channel.ChannelHandlerContext;
import io.netty.channel.ChannelOutboundHandlerAdapter;
import io.netty.channel.ChannelPromise;
import io.netty.handler.codec.http.websocketx.TextWebSocketFrame;
import org.springframework.core.annotation.Order;
import org.springframework.stereotype.Component;

/**
 * author Ken
 * create_time 2022/10/17
 */
@Component
@ChannelHandler.Sharable
@Order(-100)
public class DefaultMsgOutChannelHandler extends ChannelOutboundHandlerAdapter {

    @Override
    public void write(ChannelHandlerContext ctx, Object msg, ChannelPromise promise) throws Exception {
        if (msg instanceof Message) {
            msg = new TextWebSocketFrame(JSON.toJSONString(msg));
        }
        super.write(ctx, msg, promise);
    }
}
